<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户名称" prop="customerId">
        <el-input
          v-model="queryParams.customerId"
          placeholder="请输入客户名称"/>
      </el-form-item>
      <el-form-item label="合同名称" prop="contractName">
        <el-input
          v-model="queryParams.contractName"
          placeholder="请输入合同名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退租状态" prop="confirmStatus">
        <el-select v-model="queryParams.confirmStatus" placeholder="请选择退租状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.confirm_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="contrctrefundList" @selection-change="handleSelectionChange">
      <el-table-column fixed type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" prop="id"/>
      <el-table-column label="合同名称" align="center" prop="contractid"/>
      <el-table-column label="退租原因" align="center" prop="refundreason"/>
      <el-table-column label="退租时间" align="center" prop="refundtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.refundtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退租单号" align="center" prop="contractid"/>
      <el-table-column label="客户名称" align="center" prop="customerId"/>
      <el-table-column label="应收金额(元)" align="center" prop="ysMoney"/>
      <el-table-column label="应退金额(元)" align="center" prop="ytMoney"/>
      <el-table-column label="结算金额(元)" align="center" prop="refundMoney"/>
      <el-table-column label="退租状态" align="center" prop="confirmStatus">
        <template slot-scope="scope">
          {{scope.row.confirmStatus===1?"已退租":"未退租"}}
        </template>
      </el-table-column>
      <el-table-column width="160" fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
          >详情
          </el-button>
          <el-button
            v-show="scope.row.confirmStatus!=1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="fixrefund(scope.row)"
            v-hasPermi="['contrctrefund:contrctrefund:edit']"
          >确定退租
          </el-button>
          <el-button
            v-show="scope.row.confirmStatus !== 1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['contrctrefund:contrctrefund:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['contrctrefund:contrctrefund:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改退租对话框 -->
    <el-dialog title="退租" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="sotform" :model="sotform" :rules="sotrules" label-width="80px">

        <el-form-item label="确定退租" prop="confirmStatus">
          <!--          <el-radio v-model="form.confirmStatus" :label="3">是</el-radio>-->
          <!--          <el-radio v-model="form.confirmStatus" :label="!3">否</el-radio>-->

          <el-radio-group v-model="sotform.confirmStatus">
            <el-radio :label="3">是</el-radio>
            <el-radio :label="!3">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog title="退租" :visible.sync="updateOpen" width="500px" append-to-body>
      <el-form ref="form" :model="updateForm" :rules="rules" label-width="80px">
        <el-form-item label="应收费用" prop="ysMoney">
          <el-input v-model="updateForm.ysMoney" type="number" min="0" placeholder="请输入应收费用" @change="calculaFee">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="应退费用" prop="ytMoney">
          <el-input v-model="updateForm.ytMoney" type="number" min="0" placeholder="请输入应退费用" @change="calculaFee">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="commitUpdate">确 定</el-button>
        <el-button @click="updCannel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listContrctrefund,
    applyContrctrefund,
    delContrctrefund,
    updateContrctrefund
  } from "@/api/contrctrefund/contrctrefund";

  export default {
    name: "Contrctrefund",
    dicts: ['confirm_status'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 退租表格数据
        contrctrefundList: [],
        // 是否显示弹出层
        open: false,
        updateOpen: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          contractName: null,
          customerName: null,
          confirmStatus: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        // 退租表单参数
        sotform: {
          confirmStatus: null
        },
        // 退租表单校验
        sotrules: {
          confirmStatus: [
            {required: true, message: "请选择是否退租！", trigger: "change"}
          ]
        },
        radio: "",
        refundBillId: undefined,
        refundMoney: 0,
        updateForm: {}
      };
    },
    activated() {
      this.getList();
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询退租列表 */
      getList() {
        this.loading = true;
        listContrctrefund(this.queryParams).then(response => {
          this.contrctrefundList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          contractid: null,
          createTime: null,
          refundfile: null,
          refundreason: null,
          refundtime: null,
          updateTime: null,
          createBy: null,
          updateBy: null,
          refundMoney: null,
          ysMoney: null,
          ytMoney: null,
          customerId: null,
          confirmStatus: 0,
          confirmDate: null,
          confirmUserName: null,
          confirmId: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加退租";
      },
      /** 退租修改按钮操作 */
      fixrefund(row) {
        this.reset();
        this.refundBillId = row.id || this.ids
        this.open = true;
        this.title = "确定退租";
      },
      handleUpdate(row) {
        debugger
        this.reset();
        this.refundBillId = row.id || this.ids
        this.updateOpen = true;
        this.title = "修改退租";
        this.updateForm = row
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["sotform"].validate(valid => {
          if (valid) {
            if (this.sotform.confirmStatus === 3) {
              applyContrctrefund(this.refundBillId).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }).catch(response => {
                this.$modal.msgSuccess("修改失败");
              })
            } else {
              this.open = false;
            }

          }
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
        this.refundMoney = 0;
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除退租编号为"' + ids + '"的数据项？').then(function () {
          return delContrctrefund(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('contrctrefund/contrctrefund/export', {
          ...this.queryParams
        }, `contrctrefund_${new Date().getTime()}.xlsx`)
      },
      //合同详情
      handleDetail(row) {
        this.conductionData.refunContractId = row.id
        this.$router.push({
          path: "/contarct/ContrctreFundDetails"
        });
      },
      calculaFee() {
        var num1 = this.form.sumFee || 0;
        var num2 = this.form.sumYsFee || 0;
        if (!Object.is(num1, NaN) && !Object.is(num2, NaN)) {
          this.form.refundMoney = this.refundMoney = Math.round((num1 - num2) * 100) / 100;
        }
      },
      commitUpdate() {
        const request = {
          id: this.updateForm.id,
          ysMoney: this.updateForm.ysMoney,
          ytMoney: this.updateForm.ytMoney
        }
        updateContrctrefund(request).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.getList()
        }).catch(response => {
          this.$modal.msgError("修改失败");
          this.getList()
        })
        this.updateOpen = false
      },
      updCannel() {
        this.updateOpen = false
        this.getList()
      }
    }
  };
</script>
